/**
 Copyright (C) 2013 Yuyao Liang

 This source code is release under the BSD License.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

 Created on Oct 17, 2013 1:21:29 PM
 */
package com.googlecode.stereoplot;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.plot.dial.DialPlot;

/**
 * @author lianyuy
 * 
 */
public class StereoPlotBoreholeLayer extends StereoGridLayer {
	private double azimuth;
	private double deviation;
	private double radius = 3;

	/**
	 * @param azimuth
	 *            in rad unit
	 * @param deviation
	 *            in rad unit
	 */
	public StereoPlotBoreholeLayer(double azimuth, double deviation) {
		super();
		this.azimuth = azimuth;
		this.deviation = deviation;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jfree.chart.plot.dial.DialLayer#draw(java.awt.Graphics2D,
	 * org.jfree.chart.plot.dial.DialPlot, java.awt.geom.Rectangle2D,
	 * java.awt.geom.Rectangle2D)
	 */
	@Override
	public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
			Rectangle2D view) {

		if (deviation > Math.PI / 2)
			return;

		Rectangle2D nintyDegreeRectInner = DialPlot.rectangleByRadius(frame,
				StereoGridLayer.NintyDegreeRadius,
				StereoGridLayer.NintyDegreeRadius);
		Arc2D arcNintyyDegree = new Arc2D.Double(nintyDegreeRectInner, 0, 360,
				Arc2D.OPEN);

		g2.setPaint(this.paint);
		g2.setStroke(new BasicStroke(2.0f));

		if (plot instanceof StereoDialPlot) {
			Area area = new Area(arcNintyyDegree);
			double width = area.getBounds2D().getWidth();
			double height = area.getBounds2D().getHeight();
			double beta = height / width;
			double theta = Math.PI / 2 - azimuth;
			double a = deviation * width / Math.PI;
			double x = a * Math.cos(theta);
			double y = beta * x * Math.tan(theta);
			x = x + width / 2 + nintyDegreeRectInner.getX();
			y =- y + height / 2 + nintyDegreeRectInner.getY();
			g2.draw(markPoint(x, y));
		}
	}

	private GeneralPath markPoint(double x, double y) {
		GeneralPath path = new GeneralPath();
		path.moveTo(x - radius, y - radius);
		path.lineTo(x + radius, y - radius);
		path.lineTo(x + radius, y + radius);
		path.lineTo(x - radius, y + radius);
		path.closePath();
		return path;
	}

}
